package com.cskaoyan.com.ex;

import com.cskaoyan.com.stack.MyLinkedStack;

/**
 *
 *   括号匹配问题
 */
public class ExStack2 {
    public static void main(String[] args) {

        String str =
                "(public class ExStack2 { public static void main(String[] args) { }}";

        //        )
        // left扩容入栈
        // right出栈

        // 判断括号是否匹配
        boolean bool = stringJudgeBrackets(str);

        System.out.println(bool);

    }

    private static boolean stringJudgeBrackets(String str) {
        // 先有一个栈
        MyLinkedStack<Character> stack = new MyLinkedStack<>();

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);

            if (c == '('){
                stack.push(')');
            }else if (c == '['){
                stack.push(']');
            }else if (c == '{'){
                stack.push('}');
            }else if (c == ')' || c == '}' || c == ']'){
                if (stack.isEmpty()){
                    return false;
                }
                Character pop = stack.pop();
                if (pop != c){
                    // 出栈数据和当前遍历不一致, 不匹配
                    return false;
                }
            }
        }

        return stack.isEmpty();
    }
}
